//
//  HFCustomLayoutOne.m
//  HFObjComment
//
//  Created by 韩艳锋 on 2019/7/27.
//

#import "HFCustomLayoutOne.h"

@interface HFCustomLayoutOne ()

@property (assign, nonatomic) CGFloat collectionContentWidth;
@property (assign, nonatomic) CGFloat collectionItemHeigh;
@property (assign, nonatomic) CGFloat spaceOne;
@property (assign, nonatomic) CGFloat spaceTow;
@property (strong, nonatomic) NSMutableArray * attributesArr;

@property (assign, nonatomic) CGFloat collectionContentHeigh;

@end

@implementation HFCustomLayoutOne

- (instancetype)initWithCollectionContentWidth:(CGFloat)width itemHeigh:(CGFloat)itemHeigh spaceOne:(CGFloat)spaceOne spaceTow:(CGFloat)spaceTow
{
    self = [super init];
    if (self) {
        self.collectionContentWidth = width;
        self.collectionItemHeigh = itemHeigh;
        self.spaceOne = spaceOne;
        self.spaceTow = spaceTow;
        self.attributesArr = [NSMutableArray array];
    }
    return self;
}

- (void)prepareLayout {
    [super prepareLayout];
    self.collectionContentHeigh = 0;
    if (!self.getLengh) {
        return;
    }
    
    NSInteger itemNum = [self.collectionView numberOfItemsInSection:0];
    self.attributesArr = [NSMutableArray arrayWithCapacity:itemNum];
    CGFloat currentX = 0;
    CGFloat currentY = 0;
    for (NSInteger i = 0; i != itemNum; i ++) {
        NSIndexPath * indexpath = [NSIndexPath indexPathForRow:i inSection:0];
        UICollectionViewLayoutAttributes * att = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexpath];
        CGFloat maxX = self.getLengh(indexpath) + currentX;
        if (maxX > self.collectionContentWidth) {
            currentX = 0;
            currentY += (self.collectionItemHeigh + self.spaceTow);
        }
        att.frame = CGRectMake(currentX, currentY, self.getLengh(indexpath), self.collectionItemHeigh);
     
        currentX = CGRectGetMaxX(att.frame) + self.spaceOne;
        self.collectionContentHeigh = CGRectGetMaxY(att.frame);
        [self.attributesArr addObject:att];
    }
}

- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    
//    NSInteger staIndex = 0;
//    NSInteger endIndex = self.attributesArr.count - 1;
//    NSInteger currentIndex = (staIndex + endIndex)/2;
//    while (staIndex<endIndex) {
//        UICollectionViewLayoutAttributes * arr = self.attributesArr[currentIndex];
//    }
//
//    if (staIndex>=endIndex || currentIndex == -1) {
//        return nil;
//    }
//
    NSMutableArray * bacArr =  [NSMutableArray arrayWithCapacity:self.attributesArr.count];
    for (NSInteger i = 0; i != self.attributesArr.count; i++) {
        UICollectionViewLayoutAttributes * att = self.attributesArr[i];
        if (CGRectIntersectsRect(att.frame, rect)) {
            [bacArr addObject:att];
        } else {
            break;
        }
    }
    return bacArr;
}

- (CGSize)collectionViewContentSize {
    return CGSizeMake(self.collectionContentWidth, self.collectionContentHeigh);
}

@end
